<?php
/**
 * DormChangeMsgTpl
 *
 * @author hjc19850430@gmail.com
 */
class DormChangeMsgTpl extends BaseMsgtpl
{
    /**
     * 寝室调换管理员模板消息推送
     */
    public function messageTpl($data = '')
    {
        if($data) {
            //查询管理员openid
            $managerInfo = D('DormChangeManager')->getList(array('building_id' => $data['building_id'], 'cut_off' => 0));
            if(!$managerInfo) {
                if($data['status'] == 0) {
                    $error = array(
                        'status' => 1,
                        'log_id' => $data['log_id'],
                        'error_message' => '未绑定管理员',
                        'error_time' => time(),
                    );
                    $message = array(
                        'fans_id' => $data['fans_id'],
                        'log_id' => $data['log_id'],
                        'status' => 1,
                        'item' => '寝室调出申请',
                        'username' => $data['username'],
                    );
                } else {
                    $error = array(
                        'status' => 4,
                        'log_id' => $data['log_id'],
                        'error_message2' => '未绑定管理员',
                        'error_time2' => time(),
                    );
                    $errorMessage['manager_pass1'] = '';
                    $message = array(
                        'fans_id' => $data['fans_id'],
                        'log_id' => $data['log_id'],
                        'status' => 4,
                        'item' => '寝室调入申请',
                        'username' => $data['username'],
                    );
                }
                $this->studentInfoTpl($message);
                D('DormChangeLog')->update($error);
            } else {
                $wechat = D('Wechat')->get($this->wechat_id);
                $url = C('site_url') . U('Base/DormChange/applicationDetail', array('log_id' => $data['log_id'], 'wechat_id' => $this->wechat_id));
                $this->wxTplSign = 'EVbFClGzCgSliOtCp-s0tqbH6I134bWb4sqpsCzHkTE';

                $info = array(
                    'first' => array("value" => '您有新的' . $data['item'] . '申请，请及时处理'),
                    'keyword1' => array("value" => $data['item']),
                    'keyword2' => array("value" => $data['username']),
                    'keyword3' => array("value" => date('Y-m-d H:i:s')),
                    'remark' => array("value" => '点击详情查看'),
                );
                $point = 0;
                foreach($managerInfo as $key => $val) {
                    $openid = D('Fans')->getFields(array('wechat_id' => $this->wechat_id, 'fans_id' => $val['fans_id']), 'openid');
                    $message = $this->wxObj->messageTpl($wechat['appid'], $wechat['appsecret'], $openid, $this->wxTplSign, $info, $url);
                    if($message['status'] == 'S') {
                        ++$point;
                    }
                }
                if($point <= 0) {
                    $errorMessage = array(
                        'log_id' => $data['log_id'],
                        'error_message' => '消息未推送成功',
                    );
                    $message = array(
                        'fans_id' => $data['fans_id'],
                        'log_id' => $data['log_id'],
                        'username' => $data['username'],
                    );
                    if($data['status'] == 0) {
                        $errorMessage['status'] = 1;
                        $errorMessage['error_time'] = time();
                        $message['status'] = 1;
                        $message['item'] = '寝室调出申请';
                    } else {
                        $errorMessage['status'] = 4;
                        $errorMessage['error_time2'] = time();
                        //$errorMessage['manager_pass1'] = '';
                        $message['status'] = 4;
                        $message['item'] = '寝室调入申请';
                    }
                    $this->studentInfoTpl($message);
                    D('DormChangeLog')->update($errorMessage);
                }
            }
        }
    }

    /**
     * 学生模板消息推送
     */
    public function studentInfoTpl($data = '')
    {
        $wechat = D('Wechat')->get($this->wechat_id);
        $url = C('site_url') . U('Base/DormChange/applicationDetail', array('log_id' => $data['log_id'], 'wechat_id' => $this->wechat_id));
        $this->wxTplSign = '1SNRQ1IsyuvOgD0wqo4DEMxWwvT5sbJAHZ8ILrm0ElY';
        $info = array(
            'keyword1' => array("value" => $data['item']),
            'keyword2' => array("value" => date('Y-m-d H:i:s')),
            'remark' => array("value" => '点击详情查看'),
        );
        
        if($data['status'] == 2) {
            $info['first'] = array("value" => '你的寝室调换申请被调出管理员审核通过，请及时查看');
            $info['keyword3'] = array("value" => '调出审核通过');
        } elseif($data['status'] == 3) {
            $info['first'] = array("value" => '你的寝室调换申请被调出管理员拒绝，请及时查看');
            $info['keyword3'] = array("value" => '调出审核被拒');
        } elseif($data['status'] == 5) {
            $info['first'] = array("value" => '你的寝室调换申请被调入管理员审核通过，请及时查看');
            $info['keyword3'] = array("value" => '调入审核通过');
        } elseif($data['status'] == 6) {
            $info['first'] = array("value" => '你的寝室调换申请被调入管理员拒绝，请及时查看');
            $info['keyword3'] = array("value" => '调入审核被拒');
        } elseif($data['status'] == 1) {
            $info['first'] = array("value" => '你的寝室调换申请调出审核失败，请及时查看');
            $info['keyword3'] = array("value" => '调出审核失败');
        } elseif($data['status'] == 4) {
            $info['first'] = array("value" => '你的寝室调换申请调入审核失败，请及时查看');
            $info['keyword3'] = array("value" => '调入审核失败');
        }

        $openid = D('Fans')->getFields(array('wechat_id' => $this->wechat_id, 'fans_id' => $data['fans_id']), 'openid');
        $message = $this->wxObj->messageTpl($wechat['appid'], $wechat['appsecret'], $openid, $this->wxTplSign, $info, $url);
    }
}